package com.teatrace.dao.template.impl;

import com.teatrace.dao.BaseDao;
import com.teatrace.dao.DaoFactory;
import com.teatrace.dao.template.DaoCallBack;
import com.teatrace.dao.template.DaoTemplate;

public class AutoTransactionDaoTemplate implements DaoTemplate {

	public Object execute(DaoCallBack daoCallBack) throws Exception {
		BaseDao dao = DaoFactory.createBaseDao();
		dao.getTransaction().begin();
		Object result = null;
		try {
			result = daoCallBack.doInDao(dao);
			dao.getTransaction().commit();
		} catch (Exception e) {
			dao.getTransaction().rollback();
			throw e;
		} finally {
			if(dao != null)
				dao.close();
		}
		return result;
	}
}
